#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./paas/esb/apps/guide/component_template/hcp/get_host_list.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import json
from django import forms
from common.forms import BaseComponentForm
from components.component import Component
from .toolkit import configs

class GetHostList(Component):
    """
    api {get} /api/c/compapi/hcp/get_host_list/ get_host_list
    apiName get_host_list
    apiGroup API-HCP
    apiVersion 1.0.0
    apiDescription 查询主机列表
    
    apiParam {string} app_code app标识
    apiParam {string} app_secret app密钥
    apiParam {string} bk_token 当前用户登录态
    
    apiParam {int} app_id 业务ID
    apiParam {array} [ip_list] 主机IP地址
    
    apiParamExample {json} Request-Example:
        {
            "app_code": "esb_test",
            "app_secret": "xxx",
            "bk_token": "xxx-xxx-xxx-xxx-xxx",
            "app_id": 1,
            "ip_list": [
                {
                    "ip": "127.0.0.1",
                    "plat_id": 1,
                },
                {
                    "ip": "127.0.0.2"
                    "plat_id": 1,
                }
            ]
        }
    apiSuccessExample {json} Success-Response
        HTTP/1.1 200 OK
        {
            "result": true,
            "code": "00",
            "message": "",
            "data": [
                {
                    "inner_ip": "127.0.0.1",
                    "plat_id": 1,
                    "host_name": "db-1",
                    "maintainer": "admin",
                },
                {
                    "inner_ip": "127.0.0.2",
                    "plat_id": 1,
                    "host_name": "db-2",
                    "maintainer": "admin",
                }
            ],
        }
    """
    sys_name = configs.SYSTEM_NAME

    class Form(BaseComponentForm):
        app_id = forms.CharField(label=u'业务ID', required=True)

        def clean(self):
            data = self.cleaned_data
            return {'ApplicatioNID': data['app_id']
               }

    def handle(self):
        data = self.form_data
        data['operator'] = self.current_user.username
        try:
            response = self.outgoing.http_client.post(host=configs.host, path='/hcp/get_host_list/', data=json.dumps(data))
        except:
            response = {'code': 0,
               'data': [
                      {'inner_ip': '127.0.0.1',
                         'plat_id': 1,
                         'host_name': 'just_for_test',
                         'maintainer': 'test'
                         }]
               }

        code = str(response['code'])
        if code == '0':
            result = {'result': True,'data': response['data']
               }
        else:
            result = {'result': False,'message': response['extmsg']
               }
        self.response.payload = result
